package com.chenfan.mcn.component;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.chenfan.filestore.client.FileClient;
import com.chenfan.filestore.vo.FileResult;
import com.chenfan.mcn.extension.excel.CustomExcelExportStyler;
import com.chenfan.common.extension.spring.web.MultipartFileImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author wulg
 * @date 2024/1/5
 */
@Slf4j
@Component
public class FilestoreHelper {

    private static FileClient fileClient;

    public FilestoreHelper(@Autowired(required = false) FileClient fileClient) {
        FilestoreHelper.fileClient = Optional.ofNullable(fileClient).orElseThrow(() -> new RuntimeException("FileClient not configured bean!"));
    }

    /**
     * 上传Excel文件
     * @param fileName
     * @param clazz
     * @param dataList
     * @param <T>
     * @return
     */
    public static <T>  FileResult uploadExcel(String fileName, Class<T> clazz, List<T> dataList) {
        if(CollectionUtils.isEmpty(dataList)) {
            return null;
        }
        ByteArrayOutputStream outputStream = null;
        //上传到文件服务器
        try {
            ExportParams exportParams = new ExportParams(null, fileName);
            exportParams.setType(ExcelType.XSSF);
            exportParams.setMaxNum(1010000);
            exportParams.setSheetName(fileName);
            exportParams.setStyle(CustomExcelExportStyler.class);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, clazz, dataList);
            outputStream = new ByteArrayOutputStream();
            workbook.write(outputStream);
            return uploadExcel(fileName, outputStream);
        } catch (IOException e) {
            log.error("excel file upload failed {}", e);
        }
        return null;
    }

    public static <T>  FileResult uploadExcel(String fileName, ByteArrayOutputStream outputStream)  {
        //上传到文件服务器
        try {
            InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
            String boundary = Math.random() * 1024 + "";
            MultipartFile multipartFile = new MultipartFileImpl(fileName, fileName, "multipart/form-data; boundary=" + boundary, inputStream);
            return fileClient.uploadFile(multipartFile).getObj();
        } catch (IOException e) {
            log.error("excel file upload failed {}", e);
        } finally {
            if (Objects.nonNull(outputStream)) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
        return null;
    }
}
